package com.bw.cart.controller;

import com.bw.cart.service.CartService;
import com.bw.common.core.domain.Result;

import com.bw.common.security.utils.SecurityUtils;
import com.bw.domain.*;
import com.bw.domain.vo.CartVo;
import com.bw.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("cart")
@CrossOrigin
public class CartController {
    @Autowired
    private CartService cartService;


    /**
     * 购物车增加
     */
    @PostMapping("/addCart/{skuId}/{num}")
    public Result addCart(@PathVariable Long skuId,@PathVariable Long num)
    {
        cartService.insert(skuId,num);
        return Result.success();
    }
    /**
     * 查询购物车列表
     */
    @GetMapping("/selectCartList")
    public Result selectCartList()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<CartVo> cartVos = cartService.selectCartList(loginUser.getUserid());
        return Result.success(cartVos);
    }

    /**
     * 删除购物车
     */


    /**
     * 商品购买数量
     * @param changeNum
     * @return
     */
    @PostMapping("/changeNum")
    public Result changeNum(@RequestBody ChangeNum changeNum){
        cartService.changeNum(changeNum);
        return Result.success();
    }

    /**
     * 结算页面
     * @param settlementParameters
     * @return
     */
    @PostMapping("settlementList")
    public Result settlementList(@RequestBody SettlementParameter settlementParameters){
        List<PmsSkuInfo> list = cartService.settlementList(settlementParameters);
        return Result.success(list);
    }

    /**
     * 商品对应的优惠卷列表
     * @param skuId
     * @return
     */
    @GetMapping("couponBySettlementId/{skuId}")
    public Result couponBySettlementId(@PathVariable Long skuId){
        List<DiscountCoupon> discountCouponList = cartService.couponBySettlementId(skuId);
        return Result.success(discountCouponList);
    }


    /**
     * 优惠卷结算
     */
    @PostMapping("couponSettlement")
    public Result couponSettlement(@RequestBody CouponParameter couponParameter){
        BigDecimal sumMoney = cartService.couponSettlement(couponParameter);
        return Result.success(sumMoney);
    }

}
